package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class KickOutPopupWindow extends PopupWindow {
    private Context context;
    private View layout;
    private TextView tvSure, tvCancel, tvName;
    private String userId, userName, headerUrl;
    private RoundedCornersImage rci_my_team_header;
    private KickOut kickOut;

    public void setKickOut(KickOut kickOut) {
        this.kickOut = kickOut;
    }

    public static KickOutPopupWindow getInstance(Context context, String userId, String userName, String headerUrl) {
        return new KickOutPopupWindow(context, userId, userName, headerUrl);
    }

    private KickOutPopupWindow(Context context, String userId, String userName, String headerUrl) {
        super(context);
        this.context = context;
        this.userId = userId;
        this.userName = userName;
        this.headerUrl = headerUrl;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_kick_out, null);
        tvSure = layout.findViewById(R.id.tv_sure);
        tvCancel = layout.findViewById(R.id.tv_cancel);
        rci_my_team_header = layout.findViewById(R.id.rci_my_team_header);
        tvName = layout.findViewById(R.id.tv_name);

        tvName.setText(userName);
        GlideUtils.loadImage(context, headerUrl, rci_my_team_header);
    }


    private void initEvent() {
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (kickOut != null) {
                    kickOut.kickOut();
                }
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface KickOut {
        void kickOut();
    }
}
